package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送临时密码
 * Created by cumt on 2017/6/26.
 */

public class PersonSendCodeActivity extends BaseActivity implements View.OnClickListener {

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonSendCodeActivity.class);

        activity.startActivity(intent);
    }

    private Activity activity;

    private ImageView imgBack;
    private TextView txtTitle;


    private EditText et_now_pwd;
    private TextView btn_send_code_ok;

    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_send_code);
    }

    @Override
    public void initView() {

        activity = PersonSendCodeActivity.this;

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("發送臨時密碼");

        et_now_pwd = (EditText)findViewById(R.id.et_now_pwd);

        btn_send_code_ok= (TextView)findViewById(R.id.btn_send_code_ok);

        params = new HashMap<>();
//        params.put("mobile",user.getMobile());

        TextWatcher cname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(VilidateUtil.isMobile(String.valueOf(et_now_pwd.getText()))){

                    btn_send_code_ok.setBackgroundResource(0);
                    btn_send_code_ok.setBackgroundResource(R.drawable.btn_red_shape);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        et_now_pwd.addTextChangedListener(cname);


        initRequest();

    }

    @Override
    public void setListener() {



    }

    @Override
    public void doEvent() {

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_send_code_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                boolean isread = btn_send_code_ok.getBackground().getCurrent().getConstantState().equals(getResources().getDrawable(R.drawable.btn_red_shape).getCurrent().getConstantState());
                if(isread){

                    httpModel.startRequest();
                }

            }
        });

    }

    private void initRequest(){

        httpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.verifyUrl,params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if(VilidateUtil.isSuccess(resultCode)){

                    Toast.makeText(activity, "發送成功",
                            Toast.LENGTH_SHORT).show();
                    finish();
                }
                else{
                    Toast.makeText(activity, resultMsg+",發送失敗",
                            Toast.LENGTH_SHORT).show();
                }

            }
        },true);


    }
}
